package com.study.druid.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import sun.dc.pr.PRError;

import javax.sql.DataSource;
import java.sql.SQLException;

/**
 * @ClassName DataSourceConfig
 * @Author swj
 * @Date 2020/9/3 10:47
 */
@Configuration
public class DataSourceConfig {

    /**
     * 第一个数据源
     */
    @Value("${spring.datasource.druid.one.jdbc-url}")
    private String oneUrl;
    @Value("${spring.datasource.druid.one.username}")
    private String oneUsername;
    @Value("${spring.datasource.druid.one.password}")
    private String onePassword;
    @Value("${spring.datasource.druid.one.driver-class-name}")
    private String oneDriver;
    /**
     * 第二个数据源
     */
    @Value("${spring.datasource.druid.two.jdbc-url}")
    private String twoUrl;
    @Value("${spring.datasource.druid.two.username}")
    private String twoUsername;
    @Value("${spring.datasource.druid.two.password}")
    private String twoPassword;
    @Value("${spring.datasource.druid.two.driver-class-name}")
    private String twoDriver;

    /**
     * druid的其他配置--共用
     */
    @Value("${spring.datasource.druid.initial-size}")
    private Integer initiaSize;
    @Value("${spring.datasource.druid.max-active}")
    private Integer maxActive;
    @Value("${spring.datasource.druid.min-idle}")
    private Integer minIdle;
    @Value("${spring.datasource.druid.max-wait}")
    private Long maxWait;
    @Value("${spring.datasource.druid.pool-prepared-statements}")
    private Boolean poolPreparedStatements;
    @Value("${spring.datasource.druid.max-pool-prepared-statement-per-connection-size}")
    private Integer maxOpenPreparedStatements;
    @Value("${spring.datasource.druid.time-between-eviction-runs-millis}")
    private Integer timeBetweenEvictionRunsMillis;
    @Value("${spring.datasource.druid.validation-query}")
    private String validationQuery;
    @Value("${spring.datasource.druid.test-on-borrow}")
    private Boolean testOnBorrow;
    @Value("${spring.datasource.druid.test-on-return}")
    private Boolean testOnReturn;
    @Value("${spring.datasource.druid.test-while-idle}")
    private Boolean testWhileIdle;
    @Value("${spring.datasource.druid.filters}")
    private String filters;
    @Value("${spring.datasource.druid.connectionProperties}")
    private String connectionProperties;





    @Bean
    @Primary
    public DataSource oneDataSource() {

        //为dataresource添加配置
        DruidDataSource dataSource = new DruidDataSource();
        //druid连接必须有的配置
        dataSource.setUrl(oneUrl);
        dataSource.setUsername(oneUsername);
        dataSource.setPassword(onePassword);
        dataSource.setDriverClassName(oneDriver);

        //druid的其他配置
        dataSource.setInitialSize(initiaSize);
        dataSource.setMaxActive(maxActive);
        dataSource.setMinIdle(minIdle);
        dataSource.setMaxWait(maxWait);
        dataSource.setPoolPreparedStatements(poolPreparedStatements);
        dataSource.setMaxPoolPreparedStatementPerConnectionSize(maxOpenPreparedStatements);
        dataSource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
        dataSource.setValidationQuery(validationQuery);
        dataSource.setTestOnBorrow(testOnBorrow);
        dataSource.setTestOnReturn(testOnReturn);
        dataSource.setTestWhileIdle(testWhileIdle);
        dataSource.setConnectionProperties(connectionProperties);

        //开启sql监控
        try {
            dataSource.setFilters(filters);
        } catch (SQLException e) {
            //处理

        }
        return dataSource;

    }

    @Bean
    public DataSource twoDataSource() {
        //为dataresource添加配置
        DruidDataSource dataSource = new DruidDataSource();
        //druid连接必须有的配置
        dataSource.setUrl(twoUrl);
        dataSource.setUsername(twoUsername);
        dataSource.setPassword(twoPassword);
        dataSource.setDriverClassName(twoDriver);

        //druid的其他配置
        dataSource.setInitialSize(initiaSize);
        dataSource.setMaxActive(maxActive);
        dataSource.setMinIdle(minIdle);
        dataSource.setMaxWait(maxWait);
        dataSource.setPoolPreparedStatements(poolPreparedStatements);
        dataSource.setMaxPoolPreparedStatementPerConnectionSize(maxOpenPreparedStatements);
        dataSource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
        dataSource.setValidationQuery(validationQuery);
        dataSource.setTestOnBorrow(testOnBorrow);
        dataSource.setTestOnReturn(testOnReturn);
        dataSource.setTestWhileIdle(testWhileIdle);
        dataSource.setConnectionProperties(connectionProperties);

        //开启sql监控
        try {
            dataSource.setFilters(filters);
        } catch (SQLException e) {
            //处理

        }
        return dataSource;
    }
}
